/*-----------------------------------------
file name  : fxp_mul_pipe.v
created    : 2025/04/26 23:09:10
modified   : 2025-04-26 23:16:55
description: multiplication, pipline stage = 2
notes      : 
author     : yyrwkk
-----------------------------------------*/
module fxp_mul_pipe # (
    parameter WIIA = 8    ,
    parameter WIFA = 8    ,
    parameter WIIB = 8    ,
    parameter WIFB = 8    ,
    parameter WOI  = 8    ,
    parameter WOF  = 8    ,
    parameter ROUND= 1    
)(
    input  wire                 rst_n   ,
    input  wire                 clk     ,
    input  wire [WIIA+WIFA-1:0] ina     ,
    input  wire [WIIB+WIFB-1:0] inb     ,
    output reg  [WOI +WOF -1:0] out     ,
    output reg                  overflow
);

localparam WRI = WIIA + WIIB;
localparam WRF = WIFA + WIFB;

wire [WOI +WOF -1:0] outc     ;
wire                 overflowc;

reg  [WRI+WRF-1:0]   res      ;

// pipe1: store direct out value
always @ (posedge clk or negedge rst_n)
    if(~rst_n)
        res <= 'b0;
    else
        res <= $signed(ina) * $signed(inb);

fxp_zoom # (
    .WII      ( WRI            ),
    .WIF      ( WRF            ),
    .WOI      ( WOI            ),
    .WOF      ( WOF            ),
    .ROUND    ( ROUND          )
) res_zoom (
    .in       ( res            ),
    .out      ( outc           ),
    .overflow ( overflowc      )
);

// pipe2: store zoom out value
always @ (posedge clk or negedge rst_n)
    if(~rst_n) begin
        out      <= 'b0;
        overflow <= 1'b0;
    end else begin
        out      <= outc;
        overflow <= overflowc;
    end

endmodule

